package com.mf.utils;

import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import cn.hutool.json.JSONObject;
import com.mf.entity.bo.CallHttpBo;
import com.mf.enums.HttpMethod;
import com.mf.exceptions.GraceException;

/**
 * @Author: lmf
 * @Create: 2024/6/28 10:29
 * Description: 调用http请求
 */
public class CallHttpUtils {
    public static Object callHttp(CallHttpBo callHttpBo) {
        if (!EnumUtils.existsInEnum(HttpMethod.class, callHttpBo.getMethod(), "value")) {
            GraceException.display("请求方式错误");
        }
        JSONObject data = null;
        if (callHttpBo.getMethod().equals(HttpMethod.POST.value)) {
            data = postHttpData(callHttpBo);
        }else if(callHttpBo.getMethod().equals(HttpMethod.GET.value)){
            data = getHttpData(callHttpBo);
        }

        if (data == null) {
            return null;
        }


        // 检查响应码，只有当响应码为2000时才处理数据
        if (data.get("code").equals(2000)) {
            return findInJson(data, callHttpBo.getDataLocation());
        }

        return null;
    }
    /**
     * 发起GET请求
     * @return 响应数据
     */
    public static JSONObject getHttpData(CallHttpBo callHttpBo) {
        HttpResponse response = HttpRequest.get(callHttpBo.getUrl())
                .execute();
        if (callHttpBo.getHeaderTokenField() != null) {
            response.header(callHttpBo.getHeaderTokenField(), callHttpBo.getToken());
        }
        return new JSONObject(response.body());
    }

    /**
     * 发起POST请求
     * @return 响应数据
     */
    public static JSONObject postHttpData(CallHttpBo callHttpBo) {

        HttpRequest httpRequest = HttpRequest.post(callHttpBo.getUrl())
                .contentType("application/json")
                .body(new JSONObject(callHttpBo.getParam()).toString());

        if (callHttpBo.getHeaderTokenField() != null) {
            httpRequest.header(callHttpBo.getHeaderTokenField(), callHttpBo.getToken());
        }
        return new JSONObject(httpRequest.execute().body());
    }


    /**
     * 根据路径获取JSON对象
     * @param jsonObject JSON对象
     * @param path 路径
     * @return 找到的JSON对象，如果没有找到，返回null
     */
    public static Object findInJson(JSONObject jsonObject, String path) {
        String[] paths = path.split("\\.");

        return findInJsonRecursion(jsonObject, paths, 0);
    }

    /**
     * 递归查找JSON对象中的指定路径
     * @param jsonObject JSON对象
     * @param paths      路径数组
     * @param index      当前路径索引
     * @return 找到的JSON对象，如果没有找到，返回null
     */
    private static Object findInJsonRecursion(JSONObject jsonObject, String[] paths, int index) {
        String path = paths[index];

        if (jsonObject.containsKey(path)) {
            Object value = jsonObject.get(path);
            if (value instanceof JSONObject) {
                // 递归查找下一个部分
                return findInJsonRecursion((JSONObject) value, paths, index + 1);
            }

            if(index == paths.length - 1){
                return jsonObject.get(path);
            }
        }
        return null;
    }

}
